package com.example.controller;

import com.example.service.SendEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Map;

/**
 * @Author:徐莹
 * @Date:2025/04/25 10:26:59
 */

@RestController
@RequestMapping("/api/email")
public class SendEmailController {
    @Autowired
    private SendEmailService sendEmailService;
    
    @Autowired
    private TemplateEngine templateEngine;

    @PostMapping("/simple")
    public ResponseEntity<String> sendSimpleEmail(@RequestBody Map<String, String> emailRequest) {
        String to = emailRequest.get("to");
        String subject = emailRequest.get("subject");
        String text = emailRequest.get("text");
        
        sendEmailService.sendSimpleEmail(to, subject, text);
        return ResponseEntity.ok("Simple email sent successfully");
    }

    @PostMapping("/complex")
    public ResponseEntity<String> sendComplexEmail(@RequestBody Map<String, String> emailRequest) {
        String to = emailRequest.get("to");
        String subject = emailRequest.get("subject");
        String htmlContent = emailRequest.get("htmlContent");
        String filePath = emailRequest.get("filePath");
        String resourceId = emailRequest.get("resourceId");
        String resourcePath = emailRequest.get("resourcePath");
        
        sendEmailService.sendComplexEmail(to, subject, htmlContent, filePath, resourceId, resourcePath);
        return ResponseEntity.ok("Complex email sent successfully");
    }

    @PostMapping("/template")
    public ResponseEntity<String> sendTemplateEmail(@RequestBody Map<String, String> emailRequest) {
        String to = emailRequest.get("to");
        String subject = emailRequest.get("subject");
        String username = emailRequest.get("username");
        String vercode = emailRequest.get("vercode");
        
        Context context = new Context();
        context.setVariable("vercode", vercode);
        context.setVariable("username", username);
        
        String emailContent = templateEngine.process("emailTemplate_vercode", context);
        sendEmailService.sendTemplateEmail(to, subject, emailContent);
        
        return ResponseEntity.ok("Template email sent successfully");
    }
}
